package com.dayuanit.dy16.ebook.ebookweb.service.impl;

import com.dayuanit.dy16.ebook.ebookweb.dto.CommodityDTO;
import com.dayuanit.dy16.ebook.ebookweb.dto.NavDTO;
import com.dayuanit.dy16.ebook.ebookweb.entity.Commodity;
import com.dayuanit.dy16.ebook.ebookweb.entity.Navigation;
import com.dayuanit.dy16.ebook.ebookweb.mapper.CommodityMapper;
import com.dayuanit.dy16.ebook.ebookweb.mapper.NavigationMapper;
import com.dayuanit.dy16.ebook.ebookweb.service.INavService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class NavServiceImpl extends BaseService implements INavService {

    @Autowired
    private NavigationMapper navigationMapper;

    @Autowired
    private CommodityMapper commodityMapper;

    @Override
    public List<NavDTO> loadNav() {
        List<Navigation> navigations = navigationMapper.listAll();
        List<NavDTO> dtos = new ArrayList<>(navigations.size());

        for (Navigation nav : navigations) {
            NavDTO navDTO = new NavDTO();
            dtos.add(navDTO);

            navDTO.setLevel(nav.getLevel());
            navDTO.setNavId(nav.getId());
            navDTO.setNavName(nav.getNavigationName());
            navDTO.setParentId(nav.getParentId());
            navDTO.setLeaf(nav.getLeaf());
        }

        //一级导航栏
        List<NavDTO> firstNavList = dtos.stream().filter(nav -> nav.getLevel() == 1).collect(Collectors.toList());

        for (NavDTO firstDTO : firstNavList) {
            processNav(dtos, firstDTO);
        }
        return firstNavList;
    }

    /**
     * 递归  递：入栈  归：出站
     * 写递归要注意：退出条件务必不能忘
     *
     * 递归：在高级算法中，经常会用到，一般步骤，找出共同业务，封装到方法中，然后自己调用自己，最后写清楚退出条件接口。
     * @param allNavDTO
     * @param navDTO
     */
    private void processNav(List<NavDTO> allNavDTO, NavDTO navDTO) {
        if (navDTO.getLeaf() == 1) {
            return;
        }

        List<NavDTO> childNavList = allNavDTO.stream().filter(nav -> nav.getParentId().equals(navDTO.getNavId())).collect(Collectors.toList());
        navDTO.setChilds(childNavList);

        for (NavDTO child : childNavList) {
            processNav(allNavDTO, child);
        }
    }


    @Override
    public List<CommodityDTO> loadNavCommodity(long navId) {
        List<Commodity> commodities = commodityMapper.loadByThirdNav(navId);
        List<CommodityDTO> dtos = new ArrayList<>(commodities.size());

        for (Commodity commodity : commodities) {
            CommodityDTO commodityDTO = new CommodityDTO();
            dtos.add(commodityDTO);
            String bookName = commodity.getBookName();


            commodityDTO.setAuthor(commodity.getAuthor());
            commodityDTO.setCommoId(commodity.getId());
            commodityDTO.setCommoName(bookName.length() > 16 ? bookName.substring(0, 16) : bookName);
            commodityDTO.setPicUrl(pictureUrl + commodity.getPictureName());
            commodityDTO.setPrice(commodity.getPrice());
        }
        return dtos;
    }


    @Override
    public CommodityDTO loadCommodity(long commoId) {
        Commodity commodity = commodityMapper.selectByPrimaryKey(commoId);

        CommodityDTO commodityDTO = new CommodityDTO();
        commodityDTO.setAuthor(commodity.getAuthor());
        commodityDTO.setCommoId(commodity.getId());
        commodityDTO.setCommoName(commodity.getBookName());
        commodityDTO.setPicUrl(pictureUrl + commodity.getPictureName());
        commodityDTO.setPrice(commodity.getPrice());
        commodityDTO.setAuthor(commodity.getAuthor());
        commodityDTO.setPress(commodity.getPress());
        commodityDTO.setPublishTime(commodity.getPublishTime());
        commodityDTO.setWordNum(commodity.getWordNumber());
        commodityDTO.setIntroduce(commodity.getIntroduce());
        return commodityDTO;
    }
}
